package com.myapp.ruyixing.ui;

import static com.myapp.ruyixing.http.RYHttpClient.getMyOrderList;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.MyOrderList;
import com.myapp.ruyixing.model.MyOrderList.Order;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class OrderActivity extends BaseActivity implements OnItemClickListener {

	private MyAdapter mAdapter;
	private ArrayList<Order> mOrderList;
	private ListView mListView;
	private TextView mTitleView;
	private View mShadowsView;
	private View mMenuLayout;
	private View mLoadingView;

	private String mActiveTopCategory;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				MyOrderList list = (MyOrderList) o;
				onLoadingDataFinished(true, list);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void onLoadingDataStarted(String bduss, String type, int pn, int rn) {
		mLoadingView.setVisibility(View.VISIBLE);
		getMyOrderList(this, mHttpResponseHandler, type, bduss, pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed, MyOrderList list) {
		mLoadingView.setVisibility(View.GONE);
		if (succeed && list != null) {
			mOrderList.clear();
			if (list.list != null && list.list.size() > 0) {
				mOrderList.addAll(list.list);
			} else {
				showToast("您无订单数据！");
			}
			mAdapter.notifyDataSetChanged();
		} else {
			showToast("加载订单列表失败！");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order);
		mTitleView = (TextView) findViewById(R.id.text_title);
		mTitleView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showMenu();
			}
		});

		findViewById(R.id.img_icon).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		String nickname = LoginHelper.getInstance().getUserName();
		if (!TextUtils.isEmpty(nickname)) {
			((TextView) findViewById(R.id.user_nickname)).setText(nickname);
		}
		mListView = (ListView) findViewById(R.id.list);
		mOrderList = new ArrayList<Order>();
		mAdapter = new MyAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		mShadowsView = findViewById(R.id.shadows);
		mMenuLayout = findViewById(R.id.menus);
		mLoadingView = findViewById(R.id.my_loading);
		onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(), "shoporder", 0, 50);
	}

	public void changeType(String value, String name) {
		mActiveTopCategory = name;
		mTitleView.setText(name);
		hideMenu();

		onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(), value, 0, 50);
	}

	private void showMenu() {
		mMenuLayout.setVisibility(View.VISIBLE);
		mShadowsView.setVisibility(View.VISIBLE);
	}

	private void hideMenu() {
		mMenuLayout.setVisibility(View.GONE);
		mShadowsView.setVisibility(View.GONE);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
	}

	// private class MyAdapter extends BaseAdapter {
	//
	// private class ViewHolder {
	// public TextView mName;
	// public TextView mPrice;
	// public TextView mStatus;
	// }
	//
	// private RequestQueue mQueue;
	// private ImageLoader mImageLoader;
	//
	// public MyAdapter() {
	// super();
	// mQueue = Volley.newRequestQueue(OrderActivity.this);
	// mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());
	// }
	//
	// @Override
	// public int getCount() {
	// return mOrderList == null ? 0 : mOrderList.size();
	// }
	//
	// @Override
	// public Object getItem(int position) {
	// return mOrderList == null ? null : mOrderList.get(position);
	// }
	//
	// @Override
	// public long getItemId(int position) {
	// return position;
	// }
	//
	// @Override
	// public View getView(int position, View convertView, ViewGroup parent) {
	// ViewHolder holder = null;
	// Order item = (Order) getItem(position);
	// if (item == null) {
	// return convertView;
	// }
	// if (convertView == null) {
	// convertView = LayoutInflater.from(OrderActivity.this).inflate(
	// R.layout.order_list_item, parent, false);
	// holder = new ViewHolder();
	// holder.mName = (TextView) convertView
	// .findViewById(R.id.text_name);
	// holder.mPrice = (TextView) convertView
	// .findViewById(R.id.text_price);
	// holder.mStatus = (TextView) convertView
	// .findViewById(R.id.text_state);
	// convertView.setTag(holder);
	// } else {
	// holder = (ViewHolder) convertView.getTag();
	// }
	// if (!TextUtils.isEmpty(item.shopname)) {
	// holder.mName.setText(item.shopname);
	// }
	// if (!TextUtils.isEmpty(item.status)) {
	// holder.mStatus.setText(item.status);
	// }
	// return convertView;
	// }
	// }

	private class MyAdapter extends BaseAdapter {

		private class ViewHolder {
			TextView mShopName;
			TextView mState;
			TextView mName;
			TextView mPhone;
			TextView mDate;
			TextView mRemark;
		}

		@Override
		public int getCount() {
			return mOrderList == null ? 0 : mOrderList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return mOrderList == null ? null : mOrderList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			Order record = (Order) getItem(position);
			if (record == null) {
				return convertView;
			}
			if (convertView == null) {
				convertView = LayoutInflater.from(OrderActivity.this).inflate(
						R.layout.consumption_record_list_item, parent, false);
				holder = new ViewHolder();
				holder.mName = (TextView) (convertView
						.findViewById(R.id.text_name));
				holder.mDate = (TextView) (convertView
						.findViewById(R.id.text_time));
				holder.mShopName = (TextView) (convertView
						.findViewById(R.id.text_shop_name));
				holder.mPhone = (TextView) (convertView
						.findViewById(R.id.text_phone));
				holder.mRemark = (TextView) (convertView
						.findViewById(R.id.text_remark));
				holder.mState = (TextView) (convertView
						.findViewById(R.id.text_state));
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (!TextUtils.isEmpty(record.shopname)) {
				holder.mShopName.setText(record.shopname);
			}
			if (!TextUtils.isEmpty(record.content)) {
				holder.mRemark.setText(record.content);
			}
			if (!TextUtils.isEmpty(record.mobile)) {
				holder.mPhone.setText(record.mobile);
			}
			if (!TextUtils.isEmpty(record.name)) {
				holder.mName.setText(record.name);
			}
			if (!TextUtils.isEmpty(record.status)) {
				holder.mState.setText(record.status);
			}
			holder.mDate.setText(record.datetime);
			return convertView;
		}
	}

}
